import { throttle } from '../../utils/throttle.js'
const behavior = require('../relation_behavior')
import { HiddenCall } from '../../request/api.js'
const request = new HiddenCall
const app = getApp()
import { makePhoneCall } from "../../utils/http"
// 跟进数据组织跳转
import { describeData } from '../../utils/method.js'
Component({
  /**
    *  所有电话列表
    * - @author           王磊
    * - @date             2018-07-07
    * list                电话列表数据
    * scrollBarTop        滚动条的高度
    */
  behaviors: [behavior],
  properties: {
    list: {
      type: Object
    },
    scrollBarTop: {
      type: Number
    }
  },
  data: {
  },
  methods: {
    /**
     * 进入电话详情
     * author wl
     */
    enterPhoneDetail: throttle(function(e) {
      // 点击获取当前通话数据
      let model = e.currentTarget.dataset.item
      // 跳转进入电话详情将数据带入
      wx.navigateTo({
        url: `../../pages/phone_detail/index?model=${JSON.stringify(model)}`,
      })
    }, 2000),
    /**
      * 短信发送
      * @author
      */
    makeSms(e) {
      // 设置短信详情标志
      let resourcetype = e.currentTarget.dataset.resourcetype
      let data = {
        callType: "sms",
        detailType: '',
        utypeCode: (resourcetype === "出售" || resourcetype === "出租") ? 0 : 1,
        code: e.currentTarget.dataset.resourcecode,
        sendTel: e.currentTarget.dataset.item,
        resourcetype: resourcetype
      }
      wx.navigateTo({
        url: `../mailing_detail/index?model=${JSON.stringify(data)}`
      })
    },
    /**
    * 电话详情拨打电话
    * author dzl
    */
    smsMakePhone: throttle(function (e) {
      // 检测是否有遗留跟进未填写,有的话完善
      request.followFinished().then((res) => {
        if (res.result) {
          // 汇集数据
          describeData('phone_list', res, true, 1)
        } else {
          let code = e.currentTarget.dataset.resourcecode
          let utype = e.currentTarget.dataset.utype
          // 拿到手机号
          let called = e.currentTarget.dataset.item
          request.directCall({ called, resourceCode: code }).then(res => {
            if (res.statusCode === 200) {
              wx.hideLoading()
              // 赋值callid和手机号
              let callId = res.result.callId
              app.phonenumber = res.result.switchNumber
              // 微信api调用拨打
              makePhoneCall().then(res => {
                // 汇集数据
                let model = {}
                // 赋值callid
                model.callId = callId
                // 房客编号
                model.resourceCode = code
                // 房客类型
                model.page = "phone_list"
                model.swindex = 1
                model.utype = utype
                // 跳转进入跟进描述
                if (model.resourceCode) {
                  wx.redirectTo({
                    url: `../follow_describe/index?model=${JSON.stringify(model)}`
                  })
                }
                callId = ''
              }).catch(() => { })
              // wx.showLoading({ title: '设置拨打关系中', icon: 'loading' })
            }
            res.message !== "成功" ? wx.showToast({
              title: res.message,
              icon: 'none'
            }) : ""
          })
        }
      })
    }, 2000)
  }
})
